<template>
  <el-dialog
    width="1100px"
    :title="$t('添加取货单产品')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel">
    <el-row style="margin-bottom: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
        <el-form-item prop="skuNo">
          <el-input
            :placeholder="$t('sku编号')"
            v-model="queryParams.skuNo"
            clearable
            @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row class="mb8">
      <el-button
        type="primary"
        :disabled="tableCheck.length == 0"
        size="mini"
        v-hasPermi="['distribution:skuSellerOfflineSku:add']"
        @click="handleBatchAddAll">
        {{ $t('批量全部添加') }}
      </el-button>
    </el-row>
    <el-table
      ref="TableRef"
      :height="calTableHeight() - 150"
      :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)">
      <el-table-column fixed="left" :selectable="() => true" type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('sku')" prop="skuNo" width="150" />
      <el-table-column :label="$t('主图')" prop="imgUrl" align="center">
        <template slot-scope="{ row }">
          <el-image
            style="width: 40px; height: 40px"
            :preview-src-list="[row.imgUrl]"
            :src="row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'" />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        align="center"
        :label="$t('中文名')"
        prop="skuNameCn"
        width="150" />
      <el-table-column
        show-overflow-tooltip
        align="center"
        :label="$t('日文名')"
        prop="skuNameJp"
        width="150" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('可用库存')" prop="stock" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('最近申请价格')" prop="price">
        <template #default="{ row }">
          {{ row.price ? (+row.price).toFixed() : '' }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('取货数量')" prop="" width="150">
        <template slot-scope="{ row }">
          <el-input-number
            v-model="row.num"
            style="width: 100%"
            controls-position="right"
            :precision="0"
            :min="1"
            :max="row.stock" />
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        show-overflow-tooltip
        align="center"
        :label="$t('操作')"
        prop=""
        width="150">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            v-hasPermi="['distribution:skuSellerOfflineSku:add']"
            @click="handleAdd(row)">
            添加
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['distribution:skuSellerOfflineSku:add']"
            @click="handleAdd(row, 'all')">
            全部添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <div slot="footer">
      <el-button @click="cancel">
        {{ $t('关闭') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { skuSellerOfflineSkuAdd } from '@/api/orderManage/skuSellerOffline'
import { getSkuSellerList } from '@/api/distribution/sku/SellerSku'
export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      /**表格数据 */
      tableData: [],
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    handleBatchAddAll() {
      this.$confirm(this.$t('确认全部添加?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {
          id: this.formData.id,
          skuList: this.tableCheck.map((i) => {
            return {
              skuId: i.skuId,
              num: i.stock,
            }
          }),
        }
        skuSellerOfflineSkuAdd(data).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleAdd(row, type = 'one') {
      this.$confirm(this.$t('确认添加?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {
          id: this.formData.id,
          skuList: [
            {
              skuId: row.skuId,
              num: type == 'one' ? row.num : row.stock,
            },
          ],
        }
        skuSellerOfflineSkuAdd(data).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleSubmit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
        }
      })
      this.cancel()
    },
    getList() {
      getSkuSellerList({ ...this.queryParams, gtNum: 0 }).then((res) => {
        const { code, rows, total } = res
        if (code === 200) {
          this.tableData = rows
          this.total = total
        }
      })
    },

    cancel() {
      this.$emit('getList')
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
